package com.lujianfei.imagesearch.ui.subui;

import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import com.lujianfei.imagesearch.base.BaseChildWindow;
import com.lujianfei.imagesearch.contract.MnistImageContract;
import com.lujianfei.imagesearch.presenter.MnistImagePresenter;
import com.lujianfei.imagesearch.utils.MnistReader.MnistInfo;
import com.lujianfei.imagesearch.widget.MnistImageView;
import com.lujianfei.lib.sdautolayout.impl.SDAutoLayout;

public class MnistImageWindow extends BaseChildWindow implements MnistImageContract.View {

	MnistImageView mnistImageView = new MnistImageView();
	MnistImageContract.Presenter presenter = new MnistImagePresenter(this);
	String absolutePath;

	public MnistImageContract.Presenter getPresenter() {
		return presenter;
	}

	public MnistImageWindow() {
		super(500, 500);
		init();
	}

	@Override
	public void setTitle(String title) {
		// TODO Auto-generated method stub
		super.setTitle(title + " - " + getFileName());
	}

	private String getFileName() {
		int idx = absolutePath.lastIndexOf('\\');
		if (-1 != idx) {
			return absolutePath.substring(idx + 1);
		}
		return absolutePath;
	}

	private void init() {
		add(mnistImageView);
		SDAutoLayout.forComponent(mnistImageView).widthIs(getWidth()).heightIs(getHeight());
	}
	
	@Override
	protected void onResize() {
		// TODO Auto-generated method stub
		super.onResize();
		SDAutoLayout.forComponent(mnistImageView).widthIs(getWidth()).heightIs(getHeight());
	}

	@Override
	public Component getContext() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void setFileName(String absolutePath,MnistInfo info) {
		this.absolutePath = absolutePath;
		setTitle("显示Mnist训练图像");
		mnistImageView.setImage(absolutePath,info);
	}

	@Override
	public void updateImage() {
		mnistImageView.repaint();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (null != onCloseListener) {
			onCloseListener.onClose(this);
		}
	}
}
